<?php
function getTak(string $firstname, string $lastname) {
	$dao = MemberDAO::getInstance();
	$result = $dao->getTak($firstname, $lastname);
	if (is_null($result)) {
		return "geen lid";
	} else {
		return $result;
	}

}

function countAll(array $store) {
	$amount = 0;
	if (isset($store['bevers'])) {
		$amount += count($store['bevers']);
	}
	if (isset($store['welpen'])) {
		$amount += count($store['welpen']);
	}
	
	if (isset($store['wolven'])) {
		$amount += count($store['wolven']);
	}
	if (isset($store['jvgs'])) {
		$amount += count($store['jvgs']);
	}
	if (isset($store['vgs'])) {
		$amount += count($store['vgs']);
	}
	if (isset($store['seniors'])) {
		$amount += count($store['seniors']);
	}
	if (isset($store['leiding'])) {
		$amount += count($store['leiding']);
	}
	if (isset($store['comite'])) {
		$amount += count($store['comite']);
	}
	if (isset($store['stam'])) {
		$amount += count($store['stam']);
	}
	if (isset($store['bevers'])) {
		$amount += count($store['geen lid']);
	}
	return $amount;
}

function countTakken(array $store) {
	$amount = 0;
	if (isset($store['bevers'])) {
		$amount += count($store['bevers']);
	}
	if (isset($store['welpen'])) {
		$amount += count($store['welpen']);
	}
	
	if (isset($store['wolven'])) {
		$amount += count($store['wolven']);
	}
	if (isset($store['jvgs'])) {
		$amount += count($store['jvgs']);
	}
	if (isset($store['vgs'])) {
		$amount += count($store['vgs']);
	}
	return $amount;
}

function countTakkenIncLeiding(array $store) {
	$amount = 0;
if (isset($store['bevers'])) {
		$amount += count($store['bevers']);
	}
	if (isset($store['welpen'])) {
		$amount += count($store['welpen']);
	}
	
	if (isset($store['wolven'])) {
		$amount += count($store['wolven']);
	}
	if (isset($store['jvgs'])) {
		$amount += count($store['jvgs']);
	}
	if (isset($store['vgs'])) {
		$amount += count($store['vgs']);
	}
	if (isset($store['leiding'])) {
		$amount += count($store['leiding']);
	}
	return $amount;
}

function countKeuken(array &$store) {
	$amount = 0;
	if (isset($store['keuken'])) {
		$amount += count($store['keuken']);
		if (isset($store['leiding'])) {
			foreach ($store['leiding'] as $lentry) {
				foreach ($store['keuken'] as $key => $kentry) {
					if ($kentry->getLastname() == $lentry->getLastname()
					&& $kentry->getFirstname()==$lentry->getFirstname()) {
						$amount --;
						unset($store['keuken'][$key]);
					}
				}
			}
		}
	}
	return $amount;
}

function veggiesTakken($store) {
	$amount = 0;
	if (isset($store['bevers'])) {
		$amount += countVeggies($store['bevers']);
	}
	if (isset($store['welpen'])) {
		$amount += countVeggies($store['welpen']);
	}
	if (isset($store['wolven'])) {
		$amount += countVeggies($store['wolven']);
	}
	if (isset($store['jvgs'])) {
		$amount += countVeggies($store['jvgs']);
	}
	if (isset($store['vgs'])) {
		$amount += countVeggies($store['vgs']);
	}
	return $amount;
}

function countVeggies($entries) {
	$veggies = 0;
	if (is_array($entries)) {
		foreach ($entries as $entry) {
			foreach ($entry->getSupplements() as $supp) {
				if ($supp->getName()=="Vege") {
					$veggies++;
				}
			}
		}
	}
	return $veggies;
}

function isVeggie(Entry $entry) {
	$isVeggie = false;
	foreach ($entry->getSupplements() as $supp) {
		if ($supp->getName()=="Vege") {
			$isVeggie = true;
		}
	}
	return $isVeggie;
}

$events = array();
if ($eventid == "all") {
	$events = Event::getAllEvents();
} else {
	$events = array(Event::loadEvent((integer)$eventid));
}

foreach ($events as $event) {
	$entries = array();

	// Get all payed participants
	$total = 0;
	foreach ($event->getAllRegistrations() as $reg) {
		if ($reg->isPayed()) {
			foreach ($reg->getEntries() as $entry) {
				$entries[] = $entry;
				$total++;
			}
		}
	}



	// store in entrystore
	$entrystore = array(
					'bevers' => array(),
					'welpen' => array(),
					'wolven' => array(),
					'jvgs' => array(),
					'vgs' => array(),
					'seniors' => array(),
					'leiding' => array(),
					'keuken' => array(),
					'comite' => array(),
					'stam' => array(),
					'geen lid' => array());
	foreach ($entries as $entry) {
		// count
		$tak = getTak($entry->getFirstname(), $entry->getLastname());
		if (isset($entrystore[$tak])) {
			$entrystore[$tak][] = $entry;
		} else {
			$entrystore[$tak] = array($entry);
		}
		$groupname = $entry->getPriceCategory()->getName();
		if (strpos($groupname, "kookploeg") || strpos($groupname, "keuken")) {
			if (isset($entrystore['keuken'])) {
				$entrystore['keuken'][] = $entry;
			} else {
				$entrystore['keuken'] = array($entry);
			}
		}
	}
	?>
<div class="eventcontainer">
	<h3>
	<?php echo $event->getName();?>
	</h3>
	<h4>Totalen</h4>
	<table>
		<tr>
			<td>Totaal deelnemers:</td>
			<td><?php echo $total;?></td>
		</tr>
		<tr>
			<td>Totaal takken:</td>
			<td><?php echo countTakken($entrystore). " (".veggiesTakken($entrystore)." veggies)";?></td>
		</tr>
		<tr>
			<td>Totaal leiding:</td>
			<td><?php echo isset($entrystore['leiding']) ? count($entrystore['leiding']) : 0; echo " (".countVeggies($entrystore['leiding'])." veggies)";?></td>
		</tr>
		<tr>
			<td>Totaal keukenploeg:</td>
			<td><?php echo countKeuken($entrystore);?></td>
		</tr>
		<tr>
			<td>Totaal geen lid:</td>
			<td><?php echo isset($entrystore['geen lid']) ? count($entrystore['geen lid']) : 0;?></td>
		</tr>
	</table>

	<h4>Per categorie (1 persoon kan in meerdere categorieen vallen)</h4>
	<div class="taklist">

	<?php
	$i = 0;
	foreach ($entrystore as $tak=>$arr) {
		if (count($arr) > 0) {
			echo "<div class='takitem' id='toggle".$event->getId()."_".$i."'>".count($arr)." ".ucfirst($tak)." (waarvan ".countVeggies($arr)." veggies)</div>";
			echo "<div class='takcontent' id='takcontent".$event->getId()."_".$i."'><ul>";
			foreach ($arr as $entry) {
				echo "<li>".ucfirst($entry->getFirstname())." ".ucfirst($entry->getLastname());
				if (isVeggie($entry)) {
					echo " (Veggie!)";
				}
				echo "</li>";
			}
			echo "</ul></div>";
			?>
			<script>
			    $('#toggle<?php echo $event->getId()."_".$i;?>').click(function () {
				    $('#takcontent<?php echo $event->getId()."_".$i;?>').slideToggle('slow');
			    });
			</script>
			<?php
			$i++;
		}
	}
	?>
	</div>
	<script>
		    $('.takcontent').hide();
		</script>
</div>

	<?php
}
?>
